package com.hzlj.position.locate.controller;


import cn.hutool.core.date.DateUtil;
import com.fz.common.base.core.FormatMinuteUtil;
import com.fz.common.base.core.JsonUtils;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.office.EasyExcelUtil;
import com.hzlj.position.locate.common.common.ReadUpdateDTO;
import com.hzlj.position.locate.common.dto.reportAudio.*;
import com.hzlj.position.locate.common.enums.ReportAudioType;
import com.hzlj.position.locate.service.ReportAudioService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 【主动音频上报】接口
 *
 * @author makejava
 * @date 2024-01-15 10:49:15
 */
@Slf4j
@RestController
@RequestMapping("/position/report/audio")
public class ReportAudioController {
    @Resource
    private ReportAudioService reportAudioService;


    /**
     * 上报位置
     */
    @PutMapping("")
    public ResultDTO<Boolean> saveReportInitiative(@RequestBody ReportAudioDTO dto) {
        log.info("主动音频上报：dto={}", JsonUtils.obj2json(dto));
        this.reportAudioService.report(dto);
        return new ResultDTO<>();
    }


    /**
     * 更新为为已读
     * 第一次听音频调用
     */
    @PostMapping(value = "/read")
    public ResultDTO<Boolean> read(@RequestBody ReadUpdateDTO dto) {
        log.info("【设置已读】主动音频上报={}", dto);
        reportAudioService.read(dto);
        return new ResultDTO<>();
    }

    /**
     * 处理异常
     * 处理字典：HANDLE_AUDIO_STATUS
     */
    @PostMapping("/handle")
    public ResultDTO<Boolean> handle(@RequestBody ReportAudioHandleDTO dto) {
        log.info("【处理】主动音频上报={}", dto);
        this.reportAudioService.handle(dto);
        return ResultDTO.ok();
    }


    /**
     * 获取处理状态数据
     */
    @GetMapping(value = "/handleResult/{id}")
    public ResultDTO<ReportAudioHandleQueryResultDTO> getHandleResult(@PathVariable("id") String id, Date belongDay) {
        return new ResultDTO<>(
                reportAudioService.getHandleResult(id, belongDay)
        );
    }

    /**
     * 导出 主动音频上报
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       ReportAudioQueryParamDTO dto) {
        List<ReportAudioWithJzJbxxDTO> list = reportAudioService.listReportAudio(dto);
        ReportAudioType type = ReportAudioType.of(dto.getReportAudioType());
        String fileName = type.getTypeText();
        EasyExcelUtil.downCustomerExcel(response, fileName, fileName,
                EasyExcelUtil.Field.<ReportAudioWithJzJbxxDTO>of()
                        .add(ReportAudioWithJzJbxxDTO::getJgMc, "机构")
                        .add(ReportAudioWithJzJbxxDTO::getXm, "姓名")
                        .add(e -> DateUtil.formatDate(e.getBelongDay()), "汇报日期")
                        .add(e -> DateUtil.formatDateTime(e.getCreateTime()), "汇报时间")
                        .add(e -> FormatMinuteUtil.formatSecond(e.getAudioDuration()), "汇报时长")
                        .add(e -> {
                            if (e.getExt() == null || e.getExt().getVoiceprintMatchScore() == null) {
                                return "/";
                            }
                            return e.getExt().getVoiceprintMatchScore().setScale(2, RoundingMode.HALF_UP).toPlainString() + "%";
                        }, "声纹置信度")
                        .add(ReportAudioWithJzJbxxDTO::getReportStatusText, "情绪分析")
                        .add(type.equals(ReportAudioType.AUDIO), ReportAudioWithJzJbxxDTO::getReportAddress, "位置信息")
                        .getFields(),
                list
        );
    }


    /**
     * 列表查询 主动音频上报
     */
    @GetMapping("/list")
    public ResultDTO<List<ReportAudioWithJzJbxxDTO>> listReportAudio(ReportAudioQueryParamDTO dto) {
        log.debug("【查询无分页列表】主动音频上报,ReportAudioQueryParamDTO={}", dto);
        return new ResultDTO<>(reportAudioService.listReportAudio(dto));
    }

    /**
     * 分页查询 主动音频上报
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<ReportAudioWithJzJbxxDTO>> pageReportAudio(ReportAudioQueryParamDTO dto, PageParamDTO pageParamDTO) {
        log.debug("【查询分页列表】主动音频上报,ReportAudioQueryParamDTO={},pageParamDTO={}", dto, pageParamDTO);
        return new ResultDTO<>(reportAudioService.pageReportAudio(dto, pageParamDTO));
    }

}

